package cn.psvmc.cxzapi.controller.system;

import cn.psvmc.cxzapi.model.common.*;
import cn.psvmc.cxzapi.model.system.TRole;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.system.TRoleService;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 角色表相关操作
 * @Author auto
 * @Date 2024-08-08
 */
@RestController
@RequestMapping("/role")
public class TRoleController {

    @Resource
    private TRoleService tRoleService;

    /**
    * @Author auto 2024-08-08 新增角色表
    * @Param tRole 角色表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TRole tRole) {
        ToolUtils.validation(new Object[]{tRole.getRolename(), "角色名称", 2, tRole.getMenus(), "角色权限", 3});
        int addResult = tRoleService.add(tRole);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
    * @Author auto 2024-08-08 更新角色表
    * @Param tRole 角色表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TRole tRole) {
        ToolUtils.validation(new Object[]{tRole.getRoleid(), "id", 1, tRole.getRversion(), "版本", 1});
        int updateResult = tRoleService.edit(tRole);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-08 查询角色表详情
    * @Param tRole
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/detail")
    public Result detail(@RequestBody DetailEntity tRole) {
        Integer id = tRole.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tRoleService.detail(id));
    }

    /**
    * @Author auto 2024-08-08 删除角色表
    * @Param tRole
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity tRole) {
        Integer id = tRole.getId();
        Integer rversion = tRole.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        int delResult = tRoleService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
    * @Author auto 2024-08-08 查询角色表列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public Result list(@RequestBody SearchEntity entity) {
        return ResultGenerator.genSuccessResult("查询成功！", tRoleService.listAll(entity));
    }

    /**
     * @Description 角色选择列表
     * @Date 2024/8/11 21:49
     * @Author YWX
     * @Param [tRole]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_sel")
    public Result listForSel(@RequestBody SearchEntity entity) {
        Integer usertype = entity.getUsertype();
        ToolUtils.validation(new Object[]{usertype, "用户类型", 1});
        if (usertype.equals(2) && !ToolUtils.isTrueInteger(entity.getStoreid())) return ResultGenerator.genFailResult("商家id不能为空！");
        return ResultGenerator.genSuccessResult("查询成功！", tRoleService.listForSel(entity));
    }
}
